﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Odds.DBUtility;
using System.Data;
using Odds.Entity;

namespace Odds.DataAccess
{
    public class OddsTeamController
    {
        private string SQL_GETALLUNDELETEDTEAM = "SELECT TeamID, TeamName FROM OddsTeam WHERE " + SQL_CONDITION_UNDELETED;
        private const string SQL_CONDITION_UNDELETED = " IsDeleted = 0 ";

        public Dictionary<string, Guid> GetOddsTeamDictionary()
        {
            Dictionary<string, Guid> result = new Dictionary<string, Guid>();
            SqlDataReader dr = OddsSQLHelper.ExecuteReader(CommandType.Text, SQL_GETALLUNDELETEDTEAM, null);
            while (dr.Read())
            {
                result.Add(dr.GetString(1), dr.GetGuid(0));
            }
            return result;
        }

        public Dictionary<string, Guid> Insert(List<OddsTeam> oddsTeamSet)
        {
            Dictionary<string, Guid> result = new Dictionary<string, Guid>();
            try
            {
                StringBuilder sql = new StringBuilder();
                string teamNameList = string.Join(@"',N'", oddsTeamSet.Select(o => o.TeamName).ToArray());
                foreach (OddsTeam oddsTeam in oddsTeamSet)
                {
                    Guid guid = Guid.NewGuid();
                    sql.AppendFormat("EXEC Pro_DictionaryInsert 'OddsTeam','TeamID','TeamName',N'{0}',N'{1}';", 
                        guid, oddsTeam.TeamName);
                }
                sql.Append(@"SELECT TeamName, TeamID FROM OddsTeam WHERE TeamName IN (N'" +
                    teamNameList + @"') AND " + SQL_CONDITION_UNDELETED);
                result = OddsSQLHelper.GetStringKeyDictionary(sql.ToString());
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }
    }
}
